package com.zhong.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.auth.application.convert.AuthRolePermissionDTOConvert;
import com.zhong.auth.application.dto.AuthRolePermissionDTO;
import com.zhong.auth.common.entity.Result;
import com.zhong.auth.domain.bo.AuthRolePermissionBO;
import com.zhong.auth.domain.service.AuthRolePermissionDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限和角色controller
 */
@Slf4j
@RestController
@RequestMapping("/role_permission")
public class AuthRolePermissionController {

    @Resource
    AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增权限和角色关联
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthRolePermissionController.add.authRolePermissionDTO: {}",JSON.toJSONString(authRolePermissionDTO));
            }

            //数据校验
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()),"权限id不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),"角色id不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConvert.INSTANCE.covertDTOToBO(authRolePermissionDTO);



            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
        }catch (Exception e){
            log.error("AuthRolePermissionController.add.error: {}",e.getMessage(),e);
            return Result.fail("新增失败");
        }
    }
    /**
     * 修改权限和角色关联
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRolePermissionDTO authRolePermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthRolePermissionController.update.authRolePermissionDTO: {}",JSON.toJSONString(authRolePermissionDTO));
            }

            //数据校验
            Preconditions.checkNotNull(authRolePermissionDTO.getId(),"id不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConvert.INSTANCE.covertDTOToBO(authRolePermissionDTO);


            return Result.ok(authRolePermissionDomainService.update(authRolePermissionBO));
        }catch (Exception e){
            log.error("AuthRolePermissionController.update.error: {}",e.getMessage(),e);
            return Result.fail("修改失败");
        }
    }

    /**
     * 删除权限和角色关联
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRolePermissionDTO authRolePermissionDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("AuthRolePermissionController.delete.authRolePermissionDTO: {}",JSON.toJSONString(authRolePermissionDTO));
            }

            //数据校验
            Preconditions.checkNotNull(authRolePermissionDTO.getId(),"id不能为空");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConvert.INSTANCE.covertDTOToBO(authRolePermissionDTO);


            return Result.ok(authRolePermissionDomainService.delete(authRolePermissionBO));
        }catch (Exception e){
            log.error("AuthRolePermissionController.delete.error: {}",e.getMessage(),e);
            return Result.fail("删除失败");
        }
    }


}
